var commonUtil = require("../../../utils/commonUtil.js")
var globalData = getApp().globalData;
var httpsUrl = globalData.httpsUrl;
var imgUrl = globalData.imgUrl;
Page({

  data: {
    imgUrl: imgUrl,
    info: new Object(),
    idc1: '',//上传图片得到地址
    idc2: '',
  },
  save: function () {
    var that = this;
    var params = new Object();
    params.idc1 = this.data.idc1;
    params.idc2 = this.data.idc2;
    params.aid = wx.getStorageSync("aidKey");
    console.log("param:" + JSON.stringify(params));
    let openId = wx.getStorageSync("openidKey");
    wx.showLoading({ title: '请稍等...', })
    wx.request({
      header: { "openId": openId },
      url: httpsUrl + "/car/carData",
      dataType: "json",
      method: "POST",
      data: params,
      success: function (res) {
        wx.hideLoading();
        res = res.data;
        console.log("返回：" + JSON.stringify(res));
        if (res.code == 0) {
          wx.navigateBack({
            delta: 1
          })
        }
      }
    })
  },

  onLoad: function (options) {
    let id = wx.getStorageSync("aidKey");
    var that = this;
    var params = new Object();
    params.aid = id;
    let openId = wx.getStorageSync("openidKey");
    wx.showLoading({ title: '请稍等...', })
    wx.request({
      header: { "openId": openId, "content-Type": "application/x-www-form-urlencoded" },
      url: httpsUrl + "/car/carData",
      dataType: "json",
      method: "GET",
      data: params,
      success: function (res) {
        wx.hideLoading();
        res = res.data;
        console.log("info：" + JSON.stringify(res));
        if (res.code == 0) {
          if (!commonUtil.isEmpty(res.data)) {
            that.setData({ info: res.data })
          }
        }
      }
    })
  },

  addImg: function (e) {
    let id = e.currentTarget.id;
    var that = this;
    let openId = wx.getStorageSync("openidKey");
    wx.chooseImage({
      count: 1,  //最多可以选择的图片总数
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        //console.log(JSON.stringify(res));
        var tempFilePaths = res.tempFilePaths;
        let info = that.data.info;
        if (id == 1) {
          info.idc1 = tempFilePaths;
        } else {
          info.idc2 = tempFilePaths;
        }
        that.setData({ info: info })
        //启动上传等待中...
        wx.showToast({
          title: '正在上传...',
          icon: 'loading',
          mask: true,
          duration: 5000
        })
        var uploadImgCount = 0;
        for (var i = 0, h = tempFilePaths.length; i < h; i++) {
          console.log("tempFilePaths[i]:" + tempFilePaths[i]);
          wx.uploadFile({
            header: { "openId": openId, "Content-Type": "multipart/form-data" },
            url: httpsUrl + '/common/fileUpload',
            filePath: tempFilePaths[i],
            name: 'file',
            formData: {},
            success: function (res) {
              uploadImgCount++;
              console.log("res:" + JSON.stringify(res));
              let data = res.data;
              data = JSON.parse(data);
              console.log(data);
              if (data.code == 0) {
                var imgRetuenUrl = data.data;
                console.log("imgurl: " + imgRetuenUrl);
                if (id == 1) {
                  that.setData({ idc1: imgRetuenUrl })
                } else {
                  that.setData({ idc2: imgRetuenUrl })
                }
              } else {
                commonUtil.showModal(data.msg);
              }
              //如果是最后一张,则隐藏等待中
              if (uploadImgCount == tempFilePaths.length) {
                wx.hideToast();
              }
            },
            fail: function (res) {
              wx.hideToast();
              wx.showModal({
                title: '错误提示',
                content: '上传图片失败',
                showCancel: false,
                success: function (res) { }
              })
            }
          });
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})